<?php
class Admin_model extends CI_Model {

	public function __construct()
    {
		parent::__construct();
		$this->load->database();
    }
	
	/* Basic DB opeartion*/
    
	public function get_all_admin()
    {
		$query = $this->db->get('admin');
		return $query->result_array();
    }
  
    public function get_by_id($adm_id)
    {
		$query = $this->db->get_where('admin', array('adm_id' => $adm_id));
		return $query->row_array();
    }
  
    public function insert($data)
    {
		$this->db->insert('admin',$data);
		return $this->db->affected_rows();
	}
	
    public function update($adm_id,$adm_realname)
    {
		$this->db->update('admin', array('adm_realname'=>$adm_realname), array('adm_id' => $adm_id));
		return $this->db->affected_rows();
    }
	
	public function delete_admin($adm_id)
	{
		$this->db->delete('admin', array('adm_id' => $adm_id)); 
		return $this->db->affected_rows();
	}
	
	/*Business logic opeartion*/
	public function get_by_username($username)
	{
		$query = $this->db->get_where('admin', array('adm_username' => $username));
		return $query->row_array();
	}

	public function search($keyword,$page_no,$per_page)
	{
		$this->db->like('concat(adm_username,adm_realname)',$keyword, 'both');
		$query = $this->db->get('admin');
		$total = $query->num_rows();
		
		$this->db->like('concat(adm_username,adm_realname)',$keyword, 'both');
		$this->db->order_by('adm_id', 'desc'); 
		$this->db->limit($per_page, $per_page*($page_no-1));
		$query = $this->db->get_where('admin',array("adm_username!="=>"admin"));
		$result = $query->result_array();
		
		return array("result"=>$result,"total"=>$total);
	}

	public function get_by_system(){
		return $this->db->get_where('system',array('sys_id'=>1))->row_array();
	}

	public function update_system($data){
		$this->db->update('system',$data,array('sys_id'=>1));
		return $this->db->affected_rows();
	}
	//查看当天打卡
	public function amount_settlement(){
		$query = $this->db->get_where('callenges',array('call_time'=>date("Y-m-d")))->row_array();
        $this->db->update('callenges',array('call_is_close'=>1),array('call_id'=>$query['call_id']));
        return $query;
	}
	//查找当天成功打卡openid 更新当天所有参与记录为结束
	public function get_the_day_sign($call){

			$sql = "select * from sign_record where sr_status = 1 && sr_callenge = {$call}";
//		$si = "update sign_record set sr_is_close =1 where sr_callenge = {$call}";
//		$this->db->query($si);
		$sign = $this->db->query($sql)->result_array();
		return $sign;
	}
	//成功用户增加金额
	public function update_user($userMoney,$openid,$call){
		$this->db->set('earnMoney','earnMoney+'.$userMoney,FALSE)->set('money','money+'.$userMoney,FALSE)->where(array('openid'=>$openid))->update('users');
		$this->db->update('sign_record',array('sr_acquire_money'=>$userMoney),array('sr_user'=>$openid,'sr_callenge'=>$call));
	}

	public function update_call($surplus,$userMoney,$id){
		$this->db->update('callenges',array('call_surplus'=>$surplus,'call_user_money'=>$userMoney,'call_is_close'=>1),array('call_id'=>$id));
	}

	//change passwd
	public function update_passwd($uid,$old_pass,$password){
		$adm = $this->db->get_where('admin',array('adm_id'=>$uid))->row_array();
		if(!password_verify($old_pass,$adm['adm_password'])){
			return 1;
		}
		$this->db->update('admin',array('adm_password'=>password_hash($password,PASSWORD_DEFAULT)),array('adm_id'=>$uid));
		return 2;
	}

	//create calleng
	public function create_calleng($data){
	    $sql = "update users set siteNum = 0 where signTime != '".date('Y-m-d',strtotime('-1 day'))."'";
	    $this->db->query($sql);
		$this->db->insert('callenges',$data);
		return $this->db->affected_rows();
	}

	//select today create call
	public function today_create_call(){
		return $this->db->get_where('callenges',array('call_time'=>date('Y-m-d',strtotime('1 day'))))->result_array();
	}

	//select today close call status
	public function today_close_call(){
        return $this->db->get_where('callenges',array('call_time'=>date('Y-m-d'),'call_is_close'=>0))->result_array();
	}

	public function get_feedback(){
		return $this->db->get('feedbacks')->result_array();
	}

    public function get_no_day_sign($call){

        $sql = "select * from sign_record where sr_status = 0 && sr_callenge = {$call}";
//		$si = "update sign_record set sr_is_close =1 where sr_callenge = {$call}";
//		$this->db->query($si);
        $sign = $this->db->query($sql)->result_array();
        return $sign;
    }

}
?>
